﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Forms;
using SimpleNotify.PushLibrary;

namespace SimpleNotify.PushClient.Views
{
    class MainViewPresenter
    {
        private BindingList<string> _subscribers = new BindingList<string>();
        private NotificationType _notificationType = NotificationType.None;

        public IMainView View { get; set; }

        public MainViewPresenter(IMainView view)
        {
            this.View = view;
        }

        private void RefreshData()
        {
            try
            {
                PushLibrary.PushClient c = new PushLibrary.PushClient(Properties.Settings.Default.Server);
                _subscribers = new BindingList<string>(c.GetSubscribers().ToList());
            }
            catch (EndpointNotFoundException)
            {
                View.DisplayError("Error communicating with the Notify Service hosted at " + Properties.Settings.Default.Server + ":" + Environment.NewLine +
                    "No service listening at " + Properties.Settings.Default.Server + Environment.NewLine + Environment.NewLine +
                    "You can specify the server name in the application's config file.");
            }
            catch (Exception ex)
            {
                View.DisplayError("Error communicating with the Notify Service hosted at " + Properties.Settings.Default.Server + ":" + Environment.NewLine + ex.Message);
            }
            View.BindData(_subscribers);
            View.RefreshNotifyButtonState();                
        }

        internal void OnViewReady()
        {
            RefreshData();
        }

        internal void OnRefreshClicked()
        {
            RefreshData();
        }

        internal void OnNotifyClicked(string title, string text)
        {
            if (View.SelectedSubscribers.Count > 0)
            {
                try
                {
                    PushLibrary.PushClient c = new PushLibrary.PushClient(Properties.Settings.Default.Server);
                    PushResult[] result = c.PushMessageToMany(View.SelectedSubscribers.ToArray(), title, text, _notificationType, String.Empty);

                    string error = String.Empty;
                    foreach (var item in result)
                    {
                        if (!item.NotificationReceived)
                        {
                            error += item.ClientHostname + Environment.NewLine;
                            _subscribers.Remove(item.ClientHostname);
                        }
                    }

                    if (error.Length > 0)
                    {
                        View.DisplayWarning("Clients not available anymore: " + Environment.NewLine + error);
                    }
                }
                catch (Exception ex)
                {
                    View.DisplayError("Error communicating with the Notify Service hosted at " + Properties.Settings.Default.Server + ":" + Environment.NewLine + ex.Message);
                }
            }
        }

        internal void OnTypeChanged(string name, bool isChecked)
        {
            switch (name)
            {
                case "rdbtnError":
                _notificationType = NotificationType.Error;
                break;

                case "rdbtnOk":
                _notificationType = NotificationType.Confirmation;
                break;

                case "rdbtnInfo":
                _notificationType = NotificationType.Information;
                break;

                case "rdbtnWarning":
                _notificationType = NotificationType.Warning;
                break;

                case "rdbtnNone":
                _notificationType = NotificationType.None;
                break;

                default:
                throw new Exception("Control nicht gefunden");
            }
        }

        internal void OnInfoClicked()
        {
            AboutView v = new AboutView();
            v.ShowDialog();
        }
    }
}
